package com.e1xp.deport_management_system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.e1xp.deport_management_system.entity.Vendors;
import com.e1xp.deport_management_system.exception.CustomException;
import com.e1xp.deport_management_system.exception.CustomExceptionType;
import com.e1xp.deport_management_system.mapper.VendorsMapper;
import com.e1xp.deport_management_system.service.IUserService;
import com.e1xp.deport_management_system.service.IVendorsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author E1xP
 * @since 2021-04-09
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class VendorsServiceImpl extends ServiceImpl<VendorsMapper, Vendors> implements IVendorsService {

    @Resource
    VendorsMapper vendorsMapper;

    @Resource
    IUserService userService;

    /**
     * 获取供应商数量
     *
     * @return int 供应商数量
     */
    @Override
    public long getCount(long instanceId) {
        QueryWrapper<Vendors> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("instance_id", instanceId);
        return vendorsMapper.selectCount(queryWrapper);
    }

    /**
     * 用于获取所有供应商
     *
     * @param instanceId 实例id
     * @return List Vendors供应商列表
     */
    @Override
    public List<Vendors> getList(long instanceId) {
        QueryWrapper<Vendors> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("instance_id", instanceId);
        return vendorsMapper.selectList(queryWrapper);
    }

    public boolean isVendors(long instanceId, String vendorsName) {
        QueryWrapper<Vendors> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("instance_id", instanceId)
                .eq("name", vendorsName);
        return vendorsMapper.selectCount(queryWrapper) > 0;
    }

    /**
     * 添加供应商
     *
     * @param instanceId 实例id
     * @param vendors    供应商实体
     * @throws CustomException 当实例不存在，已有同名供应商，信息不完整时抛出该异常
     */
    @Override
    public void add(long instanceId, Vendors vendors) throws CustomException {
        this.check(instanceId, vendors);//检查信息完整
        if (this.isVendors(instanceId, vendors.getName())) {
            //已存在同名供应商
            throw new CustomException(CustomExceptionType.VENDORS_HAD_BEEN_ADD, "已存在同名供应商");
        }
        vendors.setInstanceId(instanceId);
        vendorsMapper.insert(vendors);
    }

    /**
     * 用于更新供应商实体
     *
     * @param instanceId 实例id
     * @param vendors    供应商实体
     * @throws CustomException 当实例不存在，已有同名供应商，信息不完整时抛出该异常
     */
    @Override
    public void update(long instanceId, Vendors vendors) throws CustomException {
        this.check(instanceId, vendors);//检查信息完整
        vendors.setInstanceId(instanceId);
        vendorsMapper.updateById(vendors);
    }

    /**
     * 用于检查实例是否存在、信息是否完整
     *
     * @param instanceId 实例id
     * @param vendors    供应商实体
     * @throws CustomException 当实例不存在，已有同名供应商，信息不完整时抛出该异常
     */
    @Override
    public void check(long instanceId, Vendors vendors) throws CustomException {
        if (!userService.isInstance(instanceId)) {
            //实例不存在
            throw new CustomException(CustomExceptionType.INSTANCE_NOT_FOUND_ERROR, "找不到该实例");
        }
        if (vendors.getName() == null || vendors.getName().isEmpty() ||
                vendors.getLocation() == null || vendors.getLocation().isEmpty() ||
                vendors.getContact() == null || vendors.getContact().isEmpty()
        ) {
            throw new CustomException(CustomExceptionType.USER_INPUT_ERROR, "信息不完整");
        }
    }

    /**
     * 获取就业实体
     *
     * @param instanceId 实例id
     * @param vendorsId  供应商id
     * @return Vendors
     */
    @Override
    public Vendors get(long instanceId, long vendorsId) {
        QueryWrapper<Vendors> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("instance_id", instanceId)
                .eq("id", vendorsId);
        return vendorsMapper.selectOne(queryWrapper);
    }

    /**
     * 用于获取供应商实体
     *
     * @param instanceId 实例id
     * @return List Map供应商信息
     */
    @Override
    public List<Map<String, String>> getSelection(long instanceId) {
        return vendorsMapper.getSelection(instanceId);
    }
}
